package com.quyou.funtour.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.quyou.funtour.bean.ResultInfo;
import com.quyou.funtour.enummmeration.RegionStatus;
import com.quyou.funtour.po.RegionPo;
import com.quyou.funtour.po.UserPo;
import com.quyou.funtour.service.RegionService;
import com.quyou.funtour.vo.RegionVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 专区接口
 */
@Controller
@RequestMapping("/region")
@CrossOrigin
public class RegionController {

    @Autowired
    private RegionService regionService;

    /**
     * 获取所有专区(小程序接口)
     * @return
     */
    @RequestMapping("/getAllRegion")
    @ResponseBody
    public ResultInfo getAllRegion(){
        List<RegionVo> allRegion = regionService.getAllRegion();
        return ResultInfo.ok(allRegion);
    }

    /**
     * 获取所有专区(后台管理接口)
     * @return
     */
    @RequestMapping("/getAllRegionList")
    @ResponseBody
    public ResultInfo getAllRegionList(){
        List<RegionPo> allRegion = regionService.list();
        return ResultInfo.ok(allRegion);
    }

    /**
     * 根据专区id获取专区数据(微信接口)
     * @param id 专区id
     * @return 结果对象
     */
    @RequestMapping("/getById")
    @ResponseBody
    public ResultInfo getById( String id){
        RegionPo regionPo = regionService.getRegionById(id);
        return ResultInfo.ok(regionPo);
    }

    /**
     * 根据id获取专区数据(后台管理接口)
     * @param id
     * @return
     */
    @GetMapping("/getRegionById/{rid}")
    @ResponseBody
    public ResultInfo getRegionById(@PathVariable("rid") String id){
        RegionPo regionPo = regionService.getById(id);
        return ResultInfo.ok(regionPo);
    }

    /**
     * 获取专区分页集合
     * @param current 当前页
     * @param size 每页显示条数
     * @return
     */
    @PostMapping("/regionList")
    @ResponseBody
    public ResultInfo regionList(Integer current,Integer size){
        Page<RegionPo> regionPoPage = new Page<>(current,size);
        Page<RegionPo> page = regionService.page(regionPoPage);
        return ResultInfo.ok(page);
    }

    /**
     * 添加专区
     * @param region 专区对象
     * @return
     */
    @GetMapping("/addRegion")
    @ResponseBody
    public ResultInfo addRegion(RegionPo region){
        boolean save = regionService.save(region);
        if(save){
            return ResultInfo.ok("保存成功");
        }else {
            return ResultInfo.no("保存失败");
        }
    }

    /**
     * 修改专区
     * @param region
     * @return
     */
    @GetMapping("/updateRegion")
    @ResponseBody
    public ResultInfo updateRegion(RegionPo region){
        boolean b = regionService.updateById(region);
        if(b){
            return ResultInfo.ok("修改成功");
        }else {
            return ResultInfo.no("修改失败");
        }
    }

    /**
     * 删除专区
     * @param id 专区id
     * @return
     */
    @GetMapping("/deleteRegion/{rid}")
    @ResponseBody
    public ResultInfo deleteRegion(@PathVariable("rid") String id){
        boolean b = regionService.removeById(id);
        if(b){
            return ResultInfo.ok("删除成功");
        }else {
            return ResultInfo.no("删除失败");
        }
    }

    /**
     * 模糊查询专区
     * @param current 当前页
     * @param size 每页显示条数
     * @param like 模糊规则
     * @return
     */
    @GetMapping("/likeRegion")
    @ResponseBody
    public ResultInfo likeRegion(@RequestParam("current") Integer current,
                               @RequestParam("size") Integer size,
                               @RequestParam("like")String like){
        //构建分页对象
        Page<RegionPo> userPoPage = new Page<>(current,size);
        //构建条件
        QueryWrapper<RegionPo> queryWrapper = new QueryWrapper<RegionPo>()
                .like("name", like).or()
                .like("introduce", like).or()
                .like("student_number", like).or()
                .like("brief_introduction", like).or()
                .like("contacts", like).or()
                .like("phones", like);
        //查询
        Page<RegionPo> page = regionService.page(userPoPage, queryWrapper);
        return ResultInfo.ok(page);
    }

    /**
     * 根据状态获取专区
     * @param status 状态
     * @return
     */
    @GetMapping("/getRegionByStatus")
    @ResponseBody
    public ResultInfo getRegionByStatus(@RequestParam("current") Integer current,
                                        @RequestParam("size") Integer size,
                                        @RequestParam("status") RegionStatus status){
        Page<RegionPo> userPoPage = new Page<>(current,size);
        QueryWrapper<RegionPo> queryWrapper = new QueryWrapper<RegionPo>()
                .eq("status",status);
        Page<RegionPo> page = regionService.page(userPoPage, queryWrapper);
        return ResultInfo.ok(page);
    }

}
